package com.example.websocket.controller;

import com.example.websocket.entity.AddFriendRequest;
import com.example.websocket.entity.Friend;
import com.example.websocket.entity.Group;
import com.example.websocket.entity.User;
import com.example.websocket.service.IAddFriendRequestService;
import com.example.websocket.service.IFriendService;
import com.example.websocket.service.IGroupService;
import com.example.websocket.service.IUserService;
import com.example.websocket.service.impl.AddFriendRequestServiceImpl;
import com.example.websocket.service.impl.FriendServiceImpl;
import com.example.websocket.service.impl.GroupServiceImpl;
import com.example.websocket.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    IUserService userService;

    @Autowired
    IGroupService groupService;

    @Autowired
    IFriendService friendService;

    @Autowired
    IAddFriendRequestService addFriendRequestService;

    @GetMapping({"/","/index"})
    public String toIndex(){
        return "/index";
    }

    @GetMapping("/toRegister")
    public String toRegister(){
        return "register";
    }

    @GetMapping("/space")
    public String toSpace(HttpSession session,Model model){
        User user = (User)session.getAttribute("user");
        if(user == null){
            return "index";
        }
        getInfor(user.getId(),user.getUsername(),model);
        model.addAttribute("nickname",user.getNickname());
        return "space";
    }

    @GetMapping("/logout")
    public String logout(HttpSession session){
        Object user = session.getAttribute("user");
        if(user != null){
            session.removeAttribute("user");
            return "index";
        }
        return "index";
    }

    @GetMapping("/user/{id}")
    @ResponseBody
    public ResponseEntity<User> selectUser(@PathVariable("id")int id){
        User user = userService.selectByPrimaryKey(id);
        return ResponseEntity.ok(user);
    }

    @PostMapping("/login")
    public String login(User user, Model model, HttpSession session){
        String username = user.getUsername();
        User user1 = userService.selectByUsername(username);
        if(user1 == null){
            model.addAttribute("error","用户未找到");
        }else {
            if(!user1.getPassword().equals(user.getPassword())){
                model.addAttribute("error","密码错误");
            }else {
                session.setAttribute("user",user1);
                return "redirect:/space";
            }
        }
        return "index";
    }

    @PostMapping("/user")
    public String register(User user, Model model){
        User user1 = new User();
        user1.setUsername(user.getUsername());
        user1.setPassword(user.getPassword());
        user1.setNickname(user.getNickname());
        user1.setCreatetime(new Date());
        System.out.println(new Date());
        userService.insert(user1);
        return "index";
    }

    public String getInfor(int id, String username,Model model){
        //获取自己创建群组
        List<Group> mygroups = groupService.selectByUserid(id);
        model.addAttribute("myGroups",mygroups);
        //获取所有群组
        List<Group> groups = groupService.selectAll();
        model.addAttribute("allGroups",groups);
        //获取好友列表
        List<Friend> friends = friendService.selectByUserid(id);
        ArrayList<User> users = new ArrayList<>();
        friends.forEach(friend -> {
            if(friend.getUserid() == id){
                User user = userService.selectByPrimaryKey(friend.getUserid2());
                users.add(user);
            }else{
                User user = userService.selectByPrimaryKey(friend.getUserid());
                users.add(user);
            }
        });
        model.addAttribute("friends",users);
        //获取推荐好友
        List<User> recommendUsers = userService.recommendUser();
        model.addAttribute("recommendUser",recommendUsers);
        //获取申请列表
         List<User> applyUserList = userService.getApplyUserList(id);
         model.addAttribute("applyUserList",applyUserList);
        //获取自己申请的列表
        List<User> myApplyUserList = userService.getMyApplyUserList(id);
        model.addAttribute("myApplyUserList",myApplyUserList);

        return "index";
    }


}
